﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NL.ADA.ConfigService.DAL.DatabaseSpecific;
using NL.ADA.ConfigService.DAL.EntityClasses;
using NL.ADA.ConfigService.DAL.FactoryClasses;
using NL.ADA.ConfigService.DAL.HelperClasses;
using NL.ADA.ConfigService.DAL.RelationClasses;
using NL.ADA.ConfigService.DAL;
using SD.LLBLGen.Pro.ORMSupportClasses;



namespace NL.ADA.ConfigClient
{

    public class ManagerBase<Entity> where Entity : IEntity2, new()
    {
        #region Fields
        protected Entity _entity;
        #endregion

        #region Constructors
        public ManagerBase()
        {
            DataAccessAdapter.ConnectionStringKeyName = "ConfigServiceConnectionString";
            _entity = new Entity();
        }
        #endregion

        #region Methods Get,Insert,Update,Delete

        public virtual EntityCollection Get()
        {

            IEntityFactory2 factory = ((IEntity2)_entity).GetEntityFactory();
            var result = new EntityCollection(factory);
            try
            {
                using (DataAccessAdapter adapter = new DataAccessAdapter())
                {
                    adapter.FetchEntityCollection(result,null);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;

        }

        /// <summary>
        /// Generic base-method getting the entity by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Entity of Type Entity (generic)</returns>
        public virtual Entity GetById(int id)
        {
            try
            {
                _entity.SetNewFieldValue("Id", id);
                using (DataAccessAdapter adapter = new DataAccessAdapter())
                {
                    adapter.FetchEntity(this._entity);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return this._entity;
        }
        /// <summary>
        /// Generic Base-method inserting a new Entity
        /// </summary>
        /// <param name="entity">of type Entity(generic)</param>
        /// <returns>int Id of the new Entity</returns>
        public virtual int Insert(Entity entity)
        {
            this._entity = entity;
            int result;
            try
            {
                using (DataAccessAdapter adapter = new DataAccessAdapter())
                {
                    adapter.SaveEntity(_entity, true);
                    result = (int)_entity.PrimaryKeyFields[0].CurrentValue;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;

        }
        /// <summary>
        /// Generic Base-method updating an existing entity
        /// </summary>
        /// <param name="entity">of type Entity(generic)</param>
        /// <returns>true if succeeded, false if failed</returns>
        public virtual bool Update(Entity entity)
        {
            bool result;
            try
            {
                using (DataAccessAdapter adapter = new DataAccessAdapter())
                {
                    _entity.IsNew = false;
                    result = adapter.SaveEntity(this._entity);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// Generic Base-method deleting an existing Entity
        /// </summary>
        /// <param name="id">The id of the Entity to delete</param>
        /// <returns>true if succeeded,false if failed</returns>
        public virtual bool Delete(int id)
        {
            bool result;
            Entity entity = new Entity();
            entity.SetNewFieldValue(this._entity.Fields["Id"].FieldIndex, id);
            try
            {
                using (DataAccessAdapter adapter = new DataAccessAdapter())
                {
                    result = adapter.DeleteEntity(entity);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
    }
}

